package cn.regexp.seckill.mq;

import cn.regexp.seckill.domain.OrderInfo;
import cn.regexp.seckill.mapper.OrderInfoMapper;
import cn.regexp.seckill.service.IOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Regexpei
 * @date 2024/7/5 23:55
 * @description 订单超时监听器
 */
@Component
@RocketMQMessageListener(consumerGroup = "MQTimeOutTopicListener", topic = MQConstant.ORDER_PAY_TIMEOUT_TOPIC)
@Slf4j
public class MQTimeOutTopicListener implements RocketMQListener<OrderMQResult> {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private IOrderInfoService orderInfoSevice;

    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        log.info("MQTimeOutTopicListener#onMessage --> 订单支付超时");
        // 判断订单是否支付，若是未支付，则执行以下逻辑
        // 获取订单编号
        String orderNo = orderMQResult.getOrderNo();
        if (orderNo != null) {
            // 获取订单信息
            OrderInfo orderInfo = orderInfoMapper.find(orderNo);
            // 判断订单是否处于未付款状态
            if (OrderInfo.STATUS_ARREARAGE.equals(orderInfo.getStatus())) {
                orderInfoSevice.cancelOrder(orderMQResult.getTime(), orderMQResult.getSeckillId(),
                        orderMQResult.getOrderNo());
            }
        }
    }
}
